package mn.slide.labmed;

import java.util.ArrayList;

import mn.slide.labmed.database.ItemsStatics;
import mn.slide.labmed.model.Item;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;

public class LabMedListActivity extends Activity implements OnTabChangeListener,OnClickListener
{
	ExpandableListAdapter		mAdapter;
	ArrayList<Item>				groupList;
	ArrayList<ArrayList<Item>>	childList;
	Button						zuruck;
	DisplayMetrics				metrics;
	int							DisplayWidth;
	String						one;
	@Override
	public void onCreate( Bundle savedInstanceState )
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.laborlayout);
		Button zuruck = (Button) findViewById(R.id.zurucklist);
		zuruck.setOnClickListener(this);
		ExpandableListView expan = (ExpandableListView) findViewById(R.id.ExpandableListView01);
		LayoutInflater inflater = LayoutInflater.from(getApplicationContext());
		View pathLayoutInflation = inflater.inflate(R.layout.pathlayout, expan, false);
		expan.addHeaderView(pathLayoutInflation);
		if (Pathlayout.path.size() > 0)
		{
			LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			LinearLayout pathLayout = (LinearLayout) findViewById(R.id.pathlayout);
			for (int i = 0; i < Pathlayout.path.size(); i++)
			{
				TextView path = new TextView(this);
				path.setSingleLine();
				if (i == 0)
				{
					llp.setMargins(0, 4, 0, 4);
					path.setLayoutParams(llp);
				}
				else
				{
					llp.setMargins(0, 0, 0, 4);
					path.setLayoutParams(llp);
				}
				path.setPadding(5, 1, 1, 1);
				path.setId(i);
				path.setText(Pathlayout.path.get(i));
				path.setTextColor(Color.parseColor("#12406e"));
				path.setBackgroundResource(R.drawable.title_bg);
				path.setOnClickListener(new View.OnClickListener()
				{

					public void onClick( View v )
					{
						int i = v.getId();
						if (i == 0 || i == 1 || i == 2)
						{
							String zam = Pathlayout.path.get(0);
							Pathlayout.path.clear();
							Pathlayout.path.add(zam);
						}
						else
						{
							for (int j = v.getId(); j < Pathlayout.path.size(); j++)
							{
								if (Pathlayout.path.size() >= 1)
								{
									Pathlayout.path.remove(Pathlayout.path.size() - 1);

								}

							}
						}
						HomeTabGroupActivity.group.back();
					}
				});
				pathLayout.addView(path);
			}

		}

		Bundle bundle = this.getIntent().getExtras();
		if (bundle.getString("type").equals("main"))
		{
			String modelid = bundle.getString("modelid");
			groupList = getGroupList(modelid);
			childList = getChildList(groupList);
		}
		else
		{
			String key = bundle.getString("key");
			groupList = getGroupListBySearchKey(key);
			if (groupList.size() == 0)
				groupList.add(new Item("-1", "Keine Treffer", "-1", "-1","'"));
			childList = getChildList(groupList);
		}

		mAdapter = new MyExpandableListAdapter(this, childList, groupList);
		expan.setAdapter(mAdapter);
		expan.setGroupIndicator(getResources().getDrawable(R.drawable.group_indicator));
		EditText searchbox = (EditText) findViewById(R.id.main_search);
		searchbox.setOnKeyListener(new View.OnKeyListener()
		{

			public boolean onKey( View v , int keyCode , KeyEvent event )
			{
				if (keyCode == KeyEvent.KEYCODE_ENTER)
				{
					EditText searchbox = (EditText) v;
					InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(searchbox.getWindowToken(), 0);
					HomeTabGroupActivity.group.goLabMedListActivity(searchbox.getText().toString(), "search");
				}
				return false;
			}
		});
	}

	@Override
	public void onContentChanged()
	{
		super.onContentChanged();
	}

	public ArrayList<Item> getGroupList( String modelid )
	{
		return ItemsStatics.fetchItemByParentId(modelid);
	}

	private ArrayList<Item> getGroupListBySearchKey(String key )
	{
		return ItemsStatics.fetchItemByKey(key);
	}

	public ArrayList<ArrayList<Item>> getChildList( ArrayList<Item> groupList )
	{
		ArrayList<ArrayList<Item>> childList = new ArrayList<ArrayList<Item>>();
		for (Item groupItem : groupList)
		{
			childList.add(ItemsStatics.fetchItemByParentId(groupItem.getModelId()));
		}
		return childList;
	}

	public class MyExpandableListAdapter extends BaseExpandableListAdapter
	{
		private ArrayList<ArrayList<Item>>	childList;
		private ArrayList<Item>				groupList;

		public MyExpandableListAdapter( Context context , ArrayList<ArrayList<Item>> childList , ArrayList<Item> groupList )
		{
			this.childList = childList;
			this.groupList = groupList;
		}

		public Object getChild( int groupPosition , int childPosition )
		{
			return null;
		}

		public long getChildId( int groupPosition , int childPosition )
		{

			ArrayList<Item> temp = childList.get(groupPosition);

			return Long.parseLong(temp.get(childPosition).getModelId());
		}

		public int getChildrenCount( int groupPosition )
		{
			ArrayList<Item> temp = childList.get(groupPosition);
			return temp.size();
		}

		public LinearLayout getGenericView()
		{
			metrics = new DisplayMetrics();
			getWindowManager().getDefaultDisplay().getMetrics(metrics);
			int width = metrics.widthPixels;
			DisplayWidth = width;
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width - 50, 30);
			LinearLayout layout = new LinearLayout(LabMedListActivity.this);
			layout.setBackgroundResource(R.drawable.xesun);
			layout.setOrientation(LinearLayout.HORIZONTAL);
			layout.setGravity(Gravity.CENTER_VERTICAL);
			TextView textView = new TextView(LabMedListActivity.this);
			textView.setSingleLine();
			textView.setLayoutParams(lp);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			textView.setPadding(15, 0, 0, 0);
			textView.setTextColor(Color.BLACK);
			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
			ImageView indicator = new ImageView(LabMedListActivity.this);
			indicator.setPadding(0, 10, 0, 0);
			indicator.setImageResource(R.drawable.ic_indicator_empty);
			indicator.setLayoutParams(layoutParams);
			layout.addView(textView);
			layout.addView(indicator);
			return layout;
		}

		public View getChildView( final int groupPosition , int childPosition , boolean isLastChild , View convertView , ViewGroup parent )
		{
			ArrayList<Item> tempList = childList.get(groupPosition);
			final Item temp = tempList.get(childPosition);
			LinearLayout layout = getGenericView();

			TextView textView = (TextView) layout.getChildAt(0);
			int leftPadding = DisplayWidth == 240 ? 25 : 50;
			textView.setPadding(leftPadding, 0, 0, 0);
			textView.setText(temp.getTitle());
			textView.setGravity(Gravity.CENTER_VERTICAL);
			final String modelid = temp.getModelId();
			final boolean withChild = ItemsStatics.withChild(modelid);
			if (withChild)
			{

				ImageView indicator = (ImageView) layout.getChildAt(1);
				indicator.setImageResource(R.drawable.ic_indicator_minimized);
			}

			layout.setOnClickListener(new View.OnClickListener()
			{
				public void onClick( View v )
				{
					if (withChild)
					{
						final Item group = groupList.get(groupPosition);
						Pathlayout.path.add(group.getTitle());
						Pathlayout.path.add(temp.getTitle());
						HomeTabGroupActivity.group.goLabMedListActivity(modelid, "main");
					}
					else
					{
						HomeTabGroupActivity.group.goLabMedDetailActivity(modelid);
					}
				}
			});

			return layout;
		}

		public Object getGroup( int groupPosition )
		{
			return null;
		}

		public int getGroupCount()
		{
			return groupList.size();
		}

		public long getGroupId( int groupPosition )
		{
			if (groupList.get(groupPosition).getModelId() != null)
				return Integer.parseInt(groupList.get(groupPosition).getModelId());
			else
				return 0;
		}

		public View getGroupView( int groupPosition , boolean isExpanded , View convertView , ViewGroup parent )
		{
			final Item group = groupList.get(groupPosition);
			LinearLayout layout = getGenericView();

			TextView textView = (TextView) layout.getChildAt(0);
			textView.setGravity(Gravity.CENTER_VERTICAL);
			//if(group.getTitle().)
			textView.setText(group.getTitle());

			final String modelid = group.getModelId();
			final boolean withChild = ItemsStatics.withChild(modelid);
			if (withChild)
			{
				if (isExpanded)
				{
					ImageView indicator = (ImageView) layout.getChildAt(1);
					indicator.setImageResource(R.drawable.ic_indicator_maximized);
				}
				else
				{
					ImageView indicator = (ImageView) layout.getChildAt(1);
					indicator.setImageResource(R.drawable.ic_indicator_minimized);
				}
			}
			else
			{
				layout.setOnClickListener(new View.OnClickListener()
				{

					public void onClick( View v )
					{
						if (modelid != "-1")
						{
							Pathlayout.path.add(group.getTitle());
							HomeTabGroupActivity.group.goLabMedDetailActivity(modelid);
						}
					}
				});
			}

			return layout;
		}

		public boolean isChildSelectable( int groupPosition , int childPosition )
		{
			return true;
		}

		public boolean hasStableIds()
		{
			return true;
		}
	}

	public void onTabChanged( String tabId )
	{
		if (tabId.equals("Home"))
		{
			HomeTabGroupActivity.group.goHome();
		}
	}
	@Override
	public void onBackPressed()
	{
		if (Pathlayout.path.size() == 3)
		{
			one = Pathlayout.path.get(0);
			Pathlayout.path.clear();
			Pathlayout.path.add(one);
		}
		else
		{
			Pathlayout.path.remove(Pathlayout.path.size() - 1);
		}
		HomeTabGroupActivity.group.back();
	}

	public void onClick( View v )
	{
		if (Pathlayout.path.size() == 3)
		{
			one = Pathlayout.path.get(0);
			Pathlayout.path.clear();
			Pathlayout.path.add(one);
		}
		else
		{
			if (Pathlayout.path.size() >= 1 && Pathlayout.path.size() != 3)
				Pathlayout.path.remove(Pathlayout.path.size() - 1);
		}
		HomeTabGroupActivity.group.back();
	}
}